/* vim: expandtab tabstop=4
 */
#include <stdio.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glib/gi18n.h>
#include <glib/goption.h>

#include "fwadmin-iface.h"

extern struct _fwadmin_data fwadmin_data;

static void fwadmin_dialog_setup_add_button(GladeXML *);    
static void fwadmin_dialog_setup_delete_button(GladeXML *);    
static void fwadmin_dialog_setup_edit_button(GladeXML *);    
static void fwadmin_dialog_setup_ok_button(GladeXML *);    
static void fwadmin_dialog_setup_close_button(GladeXML *);
    
static void fwadmin_dialog_setup_connections_combo(GladeXML *);

static void fwadmin_dialog_setup_rules_list(GladeXML *);

static void fwadmin_shutdown();
static void fwadmin_apply();

static struct {
    GtkWidget *add_button;
    GtkWidget *delete_button;
    GtkWidget *edit_button;
    GtkWidget *ok_button;
    GtkWidget *close_button;

    GtkWidget *connections_combo;
    GtkWidget *rules_list;
} fwadmin_dialog;


void fwadmin_setup_interface() {
    GladeXML *xml;

    xml = glade_xml_new("gnome-firewall-admin.glade", NULL, NULL);

    fwadmin_dialog_setup_add_button(xml);    
    fwadmin_dialog_setup_delete_button(xml);    
    fwadmin_dialog_setup_edit_button(xml);    
    fwadmin_dialog_setup_ok_button(xml);    
    fwadmin_dialog_setup_close_button(xml);
    
    fwadmin_dialog_setup_connections_combo(xml);

    fwadmin_dialog_setup_rules_list(xml);
}

static void fwadmin_dialog_setup_add_button(GladeXML *xml) {
    fwadmin_dialog.add_button = glade_xml_get_widget(xml, "add_button");
}

static void fwadmin_dialog_setup_delete_button(GladeXML *xml) {
    fwadmin_dialog.delete_button = glade_xml_get_widget(xml, "delete_button");
}

static void fwadmin_dialog_setup_edit_button(GladeXML *xml) {
    fwadmin_dialog.edit_button = glade_xml_get_widget(xml, "edit_button");
}

static void fwadmin_dialog_setup_ok_button(GladeXML *xml) {
    fwadmin_dialog.ok_button = glade_xml_get_widget(xml, "ok_button");

    g_signal_connect(fwadmin_dialog.ok_button, "clicked", G_CALLBACK(fwadmin_apply), NULL);
}

static void fwadmin_dialog_setup_close_button(GladeXML *xml) {
    fwadmin_dialog.close_button = glade_xml_get_widget(xml, "close_button");

    g_signal_connect(fwadmin_dialog.close_button, "clicked", G_CALLBACK(fwadmin_shutdown), NULL);
}

static void fwadmin_dialog_setup_connections_combo(GladeXML *xml) {
    GList *iface_names = NULL, *list;
    GtkListStore *store;
    GtkTreeIter iter;
    GtkCellRenderer *renderer;
    GtkWidget *combo;
    gchar *data;
    GError *error;

    combo = glade_xml_get_widget(xml, "connections_combo");

    fwadmin_dialog.connections_combo = combo;

    store = gtk_list_store_new(1, G_TYPE_STRING);

    gtk_combo_box_set_model(GTK_COMBO_BOX(combo), GTK_TREE_MODEL(store));

    iface_names = fwadmin_list_interface_names(&error);

    if(iface_names == NULL)
        return;

    for(list = iface_names; list != NULL; list = g_list_next(list)) {
        data = list->data;

        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter, 0, g_strdup(data), -1);

//        g_free(data);
    }

    renderer = gtk_cell_renderer_text_new();
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, FALSE);
    gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), renderer, "text", 0, NULL);
}

static void fwadmin_dialog_setup_rules_list(GladeXML *xml) {
    fwadmin_dialog.rules_list = glade_xml_get_widget(xml, "rules_list");
}

static void fwadmin_apply() {
    // apply rules
    fwadmin_shutdown();
}

static void fwadmin_shutdown() {
    gtk_exit(0);
}
